package com.ebelter.seneo.ui.fragments.main;


import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.common.IOrder;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserCurrentMeasureRecord;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoBTManager;
import com.ebelter.seneo.models.SeneoDbUtils;
import com.ebelter.seneo.ui.activitys.FitbitActivity;
import com.ebelter.seneo.ui.activitys.GoogleFitActivity;
import com.ebelter.seneo.ui.activitys.SeneoLoginActivity;
import com.ebelter.seneo.ui.activitys.SeneoMainActivity;
import com.ebelter.seneo.ui.activitys.SeneoMembersListActivity;
import com.ebelter.seneo.ui.activitys.WebActivity;
import com.ebelter.seneo.ui.activitys.sets.AboutActivity;
import com.ebelter.seneo.ui.activitys.sets.DeviceManagerActivity;
import com.ebelter.seneo.ui.activitys.sets.SwitchUnitActivity;
import com.ebelter.seneo.ui.fragments.BaseFragment;
import com.ebelter.seneo.ui.fragments.infos.Info_BMI_Fragment;
import com.ebelter.seneo.ui.views.SeneoTipDialog2;
import com.ebelter.seneo.utils.SeneoUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


//填写密码
public class Set_Fragment extends BaseFragment  implements  View.OnClickListener {

    public static final String TAG = "Set_Fragment";
    RelativeLayout set_switchunit_rl;
    RelativeLayout set_members_rl;
    RelativeLayout set_help_rl;
    RelativeLayout set_googlefit_rl;

    RelativeLayout set_fit_rl;
    RelativeLayout set_sumsung_rl;
    RelativeLayout set_devicemanagement_rl;
    RelativeLayout set_clearinfo_rl;
    RelativeLayout set_update_rl;
    RelativeLayout set_exit_rl;

    TextView set_unit_tv;
    @Override
    protected int setLayoutResouceId() {
        return R.layout.m_set_fly;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        FV();
        displayUnit();
    }

    private void FV() {
        set_switchunit_rl=findViewById(R.id.set_switchunit_rl);
        set_members_rl=findViewById(R.id.set_members_rl);
        set_googlefit_rl=findViewById(R.id.set_googlefit_rl);
        set_help_rl=findViewById(R.id.set_help_rl);

        set_fit_rl=findViewById(R.id.set_fit_rl);
        set_sumsung_rl=findViewById(R.id.set_sumsung_rl);
        set_devicemanagement_rl=findViewById(R.id.set_devicemanagement_rl);
        set_clearinfo_rl=findViewById(R.id.set_clearinfo_rl);
        set_update_rl=findViewById(R.id.set_update_rl);
        set_exit_rl=findViewById(R.id.set_exit_rl);
        set_unit_tv=findViewById(R.id.set_unit_tv);
    }


    @Override
    protected void setListener() {
        set_switchunit_rl.setOnClickListener(this);
        set_members_rl.setOnClickListener(this);
        set_googlefit_rl.setOnClickListener(this);
        set_help_rl.setOnClickListener(this);

        set_fit_rl.setOnClickListener(this);
        set_sumsung_rl.setOnClickListener(this);
        set_devicemanagement_rl.setOnClickListener(this);
        set_clearinfo_rl.setOnClickListener(this);
        set_update_rl.setOnClickListener(this);
        set_exit_rl.setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }





    private void displayUnit(){
        ViewUtils.setTextViewStr(set_unit_tv, SeneoUtils.getUnitModel()==0?"kg":"lb");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, "Set_Fragment--onEventMainThread接收到CommonEventBus信息--" + event.toString());

        if (event.cmd== IUserContant.ICMDS.UPDATE_UNIT_DISPLAY){
            displayUnit();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.set_switchunit_rl:
                startActivity(new Intent(getActivity(),SwitchUnitActivity.class));
                break;
            case R.id.set_members_rl:
                startActivity(new Intent(getActivity(),SeneoMembersListActivity.class));
                break;
            case R.id.set_help_rl:
                WebActivity.openWebActivity(getActivity(),"file:///android_asset/help/index.htm","Help");
                break;
            case R.id.set_googlefit_rl:
                startActivity(new Intent(getActivity(),GoogleFitActivity.class));
                break;
            case R.id.set_devicemanagement_rl:
                startActivity(new Intent(getActivity(),DeviceManagerActivity.class));
                break;
            case R.id.set_clearinfo_rl:
                showClearDialog();
                break;
            case R.id.set_update_rl:
                startActivity(new Intent(getActivity(),AboutActivity.class));
                break;
            case R.id.set_exit_rl:
                exit();
                break;
            case R.id.set_fit_rl:
                startActivity(new Intent(getActivity(),FitbitActivity.class));
                break;
        }
    }

    //显示清除用户信息的对话框
    private void showClearDialog() {


        SeneoTipDialog2 tipDialog2=new SeneoTipDialog2(getContext(), "This will delete all user infomation and measurement records on your scale.Clear?");
        tipDialog2.setListener(new SeneoTipDialog2.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView, String str) {

                if (getActivity()!=null){
                    SeneoBTManager mSeneoBTManager=((SeneoMainActivity)getActivity()).getSeneoBTManager();
                    if (mSeneoBTManager!=null){
                        if (!mSeneoBTManager.isConnect()){
                            ToastUtil.show("Unable to connect to the scale. Please lightly step on your Body Fat Scale to activate it.");
                            return;
                        }else {

                            UserSpUtil.writeLong(IUserContant.IUser.clearUserTime,System.currentTimeMillis());
                            mSeneoBTManager.clearAllUserInfo();
                            ToastUtil.show("User information cleared.");//清空用户信息成功
                        }

                    }
                }
                dialogView.cancel();
            }
        });
        tipDialog2.show();



    }

    private void exit() {
        SeneoTipDialog2 tipDialog2=new SeneoTipDialog2(getContext(),"Are you sure you want to sign out?");
        tipDialog2.setListener(new SeneoTipDialog2.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView, String str) {
                SeneoDbUtils.deleteTable(SeneoUserinfos.class);
                SeneoDbUtils.deleteTable(SeneoUserCurrentMeasureRecord.class);
               // UserSpUtil.writeString(IUserContant.IUser.loginName,null);
                //UserSpUtil.writeString(IUserContant.IUser.psw,null);
                dialogView.cancel();

                if (getActivity()!=null){
                    startActivity(new Intent(getActivity(), SeneoLoginActivity.class));
                    getActivity().finish();
                }

            }
        });
        tipDialog2.show();
    }
}
